﻿using System;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.IO;
using DbUp.Helpers;

namespace DbUp.SqlCe.Helpers
{
    /// <summary>
    /// Used to create databases that are deleted at the end of a unit test.
    /// </summary>
    public class SqlCeTemporaryDatabase : IDisposable, ITemporaryDatabase
    {
        private readonly string connectionString;
        private readonly IAdHocSqlRunner database;
        private FileInfo databaseFile;
        private readonly string databaseName;

        /// <summary>
        /// Initializes a new instance of the <see cref="SqlCeTemporaryDatabase"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public SqlCeTemporaryDatabase(string name)
        {
            databaseName = Path.Combine(Environment.CurrentDirectory, name + ".sdf");
            connectionString = string.Format("Data Source={0}", databaseName);
            database = new SqlCeAdHocSqlRunner(connectionString);
        }

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public string ConnectionString
        {
            get { return connectionString; }
        }

        /// <summary>
        /// Gets a tool to run ad-hoc SQL queries.
        /// </summary>
        /// <value>The ad hoc.</value>
        public IAdHocSqlRunner AdHoc
        {
            get { return database; }
        }

        /// <summary>
        /// Creates the database.
        /// </summary>
        public void Create()
        {
            var existingFile = new FileInfo(this.databaseName);
            if (existingFile.Exists)
            {
                existingFile.Delete();
            }

            using (var sqlCeEngine = new SqlCeEngine(this.ConnectionString))
            {
                sqlCeEngine.CreateDatabase();

                this.databaseFile = new FileInfo(this.databaseName);
            }
        }

        /// <summary>
        /// Deletes the database.
        /// </summary>
        public void Dispose()
        {
            this.databaseFile.Delete();
        }

        internal class TraceLog : ILog
        {
            public void WriteInformation(string format, params object[] args)
            {
                Trace.TraceInformation(format, args);
            }

            public void WriteError(string format, params object[] args)
            {
                Trace.TraceError(format, args);
            }

            public void WriteWarning(string format, params object[] args)
            {
                Trace.TraceWarning(format, args);
            }

            public IDisposable Indent()
            {
                return new FooDisposable();
            }


            public class FooDisposable : IDisposable
            {
                public void Dispose()
                {
                }
            }
        }
    }
}